package com.prb.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.prb.factory.ConnectionFactory;

import java.sql.Connection;
import java.sql.SQLException;

public class ConnectionUtils {


    public  static DruidDataSource dataSource;
    static{
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
    }

    public static Connection getConnection() throws SQLException {

        Connection connection = ConnectionFactory.connectionThreadLocal.get();
        if(connection == null){
            connection = dataSource.getConnection();
            ConnectionFactory.connectionThreadLocal.set(connection);
        }

        return connection;
    }

}
